package com.ruoyi.jproom.service.impl;

import com.ruoyi.jproom.entity.Housing;
import com.ruoyi.jproom.entity.Residential;
import com.ruoyi.jproom.entity.Sell;
import com.ruoyi.jproom.mapper.ImageInfoMapper;
import com.ruoyi.jproom.mapper.SellMapper;
import com.ruoyi.jproom.service.SellService;
import com.ruoyi.jproom.utils.BaseResult;
import com.ruoyi.jproom.utils.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zeo
 * @explain 出售服务实现类
 * @date 2020/7/27 15:03
 */
@Service
public class SellServiceImpl implements SellService {

    @Autowired
    private SellMapper sellMapper;

    @Autowired
    private ImageInfoMapper imageInfoMapper;


    @Override
    public BaseResult findAllRecommend(Residential residential, Housing housing, Sell sell) {
        List<Sell> sells = sellMapper.findAllRecommend(residential,housing,sell);
        if (sells.size() == 0) {
            return BaseResult.build(ResultEnum.SELECT_NULL);
        } else {
            //数据库逗号分隔成数组处理
            for (Sell sell1 : sells) {
                sell1.getResidential().setImageInfoList(imageInfoMapper.findImageInfoListBySellId(sell1.getResidential().getResidentialId()));
                sell1.getHousing().setImageInfoList(imageInfoMapper.findImageInfoListBySellId(sell1.getHousing().getHousingId()));

                if(sell1.getSellLabel() != null) {
                    sell1.setSellLabels(sell1.getSellLabel().split(","));
                }
            }
            return BaseResult.build(ResultEnum.SUCCESS, sells);
        }
    }

    @Override
    public Sell findSellInfoByHousingId(String housingId) {
        return sellMapper.findSellInfoByHousingId(housingId);
    }

    @Override
    public int addSellInfo(Sell sell) {
        return sellMapper.addSellInfo(sell);
    }

    @Override
    public int changeIsDeal(String housingId, int isDeal) {
        return sellMapper.changeIsDeal(housingId,isDeal);
    }
}
